package se.recept.kollen.model;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author jocke
 */
@Entity
@Table(name = "Product")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = Product.FIND_ALL, query = "SELECT r FROM Product r"),
    @NamedQuery(name = Product.FIND_BY_ACTIVE, query = "SELECT r FROM Product r WHERE r.active = :active"),
    @NamedQuery(name = Product.FIND_BY_LIKE_NAME, query = "SELECT r FROM Product r WHERE r.name LIKE :name"),
    @NamedQuery(name = "Product.findById", query = "SELECT r FROM Product r WHERE r.id = :id"),
    @NamedQuery(name = "Product.findByName", query = "SELECT r FROM Product r WHERE r.name = :name")})
public class Product implements Serializable {

    public static final String FIND_ALL = "Product.findAll";
    public static final String FIND_BY_ACTIVE = "Product.findByActive";
    public static final String FIND_BY_LIKE_NAME = "Product.findByLikeName";

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "ID")
    private Integer id;

    @JoinTable(name = "Product_Ingredient", joinColumns = {
        @JoinColumn(name = "PRODUCT_ID", referencedColumnName = "ID")}, inverseJoinColumns = {
        @JoinColumn(name = "INGREDIENT_ID", referencedColumnName = "ID")})
    @ManyToMany
    private List<Ingredient> ingredients;

    @JoinColumn(name = "CUSTOMER_ID", referencedColumnName = "ID")
    @ManyToOne(optional = false)
    private Customer customer;

    private static final long serialVersionUID = 1L;

    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 60)
    @Column(name = "NAME")
    private String name;

    @JoinColumn(name = "CATEGORY_ID", referencedColumnName = "ID")
    @ManyToOne(optional = false)
    private ProductCategory category;

    @NotNull
    @Size(min = 1, max = 50)
    @Basic(optional = false)
    @Column(name = "PLU")
    private String plu;

    @NotNull
    @Basic(optional = false)
    @Column(name = "ACTIVE")
    private boolean active;

    @NotNull
    @Column(name = "VERIFIED")
    @Basic(optional = true)
    @Temporal(TemporalType.TIMESTAMP)
    private Date verified;

    @Basic(optional = false)
    @Column(name = "QUANTITY")
    private Integer quantity;

    @ManyToOne
    private AppUser verifiedBy;

    @Basic(optional = false)
    @Column(name = "PRICE")
    private Integer Price;

    public Integer getQuantity() {
        return quantity;
    }

    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }

    public Product() {
    }

    public Product(Integer id) {
        this.id = id;
    }

    public Product(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Ingredient> getIngredients() {
        return ingredients;
    }

    public void setIngredients(List<Ingredient> ingredients) {
        this.ingredients = ingredients;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public ProductCategory getCategory() {
        return category;
    }

    public void setCategory(ProductCategory category) {
        this.category = category;
    }

    public String getPlu() {
        return plu;
    }

    public void setPlu(String plu) {
        this.plu = plu;
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public Date getVerified() {
        return verified;
    }

    public void setVerified(Date verified) {
        this.verified = verified;
    }

    public AppUser getVerifiedBy() {
        return verifiedBy;
    }

    public void setVerifiedBy(AppUser verifiedBy) {
        this.verifiedBy = verifiedBy;
    }

    public Integer getPrice() {
        return Price;
    }

    public void setPrice(Integer Price) {
        this.Price = Price;
    }
    
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Product)) {
            return false;
        }
        Product other = (Product) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "se.recept.kollen.model.Recipe[ id=" + id + " ]";
    }
}
